<template>
  <view class="home">
    <view class="content">
      <view class="search">
        <TabPopup :options="tabOptions" @chooseItem="handleChooseItem" />
        <view class="inputBox" @click="changeToSeach">
          <image src="../../static/icons/search.png" class="seachIcon"></image>
          <text>搜索{{ tabOptions[tabIndex].slice(1, 3) }}</text>
        </view>
      </view>

      <view class="tabs">
        <van-tabs
          :active="0"
          :z-index="hiddenTab ? '0' : '1'"
          animated
          swipeable
          color="#05A47D"
        >
          <van-tab title="伙伴区">
            <view class="contentBox">
              <UserList
                v-if="!matchFlag"
                @showUserIntro="handleShowUserIntro"
              />
              <UserTagsList
                v-else
                ref="userMatchTagsListRef"
                @showUserIntro="handleShowUserIntro"
              />
            </view>
          </van-tab>
          <van-tab title="讨论区">
            <view class="contentBox">
              <DiscussionList />
            </view>
          </van-tab>
        </van-tabs>
      </view>

      <view class="userMask">
        <van-overlay :show="userMaskFlag">
          <view class="wrapper">
            <UserIntro
              @sendCloseMask="userMaskFlag = false"
              :userDetail="userDetail"
            />
          </view>
        </van-overlay>
      </view>
    </view>
    <MatchOperator @changeSwitchStatus="handleSwitchChange" />
  </view>
</template>

<script>
import UserList from "./components/UserList.vue";
import UserTagsList from "./components/UserTagsList.vue";
import DiscussionList from "./components/DiscussionList.vue";
import TabPopup from "./components/TabPopup.vue";
import UserIntro from "../../components/home/UserIntro.vue";
import MatchOperator from "../../components/public/MatchOperator.vue";

export default {
  name: "Home",
  components: {
    UserList,
    UserTagsList,
    DiscussionList,
    TabPopup,
    UserIntro,
    MatchOperator,
  },
  props: {
    hiddenTab: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      name: "home",
      matchFlag: false,
      userMaskFlag: false,
      tabOptions: ["找伙伴", "找讨论", "找队伍"],
      tabIndex: 0,
      userDetail: {},
    };
  },
  beforeDestroy() {
    console.log("离开");
    this.$store.commit("clearUserList");
    this.$store.commit("m_discussion/clearDiscussionList");
  },
  methods: {
    handleSwitchChange(status) {
      this.matchFlag = !this.matchFlag;
      if (status) {
        this.$nextTick(() => {
          this.$refs.userMatchTagsListRef.getUserListData();
        });
      }
    },

    // 展示用户详细信息
    handleShowUserIntro(userDetail) {
      this.userDetail = userDetail;
      this.userMaskFlag = true;
    },
    changeToSeach() {
      console.log("跳转至搜索页：", this.tabIndex);
      uni.navigateTo({
        url: `/pages/Search/Search?type=${this.tabIndex}`,
      });
    },
    handleChooseItem(index) {
      this.tabIndex = index;
    },
  },
};
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  .content {
    display: flex;
    flex-direction: column;
    align-items: center;
    .search {
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: #fff;
      width: 100vw;
      .inputBox {
        .seachIcon {
          width: 35rpx;
          height: 35rpx;
          margin-right: 10rpx;
        }
        display: flex;
        align-items: center;
        height: 75rpx;
        padding-left: 30rpx;
        color: #aaa;
        width: 65vw;
        margin-top: 20rpx;
        border-radius: 10rpx;
        border: 1px solid #ccc;
        box-shadow: 0 0 6px rgba(200, 200, 200, 0.7);
        background-color: #fff;
        font-size: 30rpx;
      }
    }
    .tabs {
      width: 100vw;
    }
    .contentBox {
      width: 100vw;
    }
  }
  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
  }
}
</style>
